﻿namespace SAD.ClientLogic
{
    using System;
    using SAD.Common.Contracts.ShowNode;
using SAD.ShowNodeClientLib;
    using System.Collections.Generic;
    using System.ServiceModel;

    public class PushInfoReceiverService : IPushInfoReceiver
    {
        #region IInfoReceiver 成员

        public void AddInfo(SAD.Common.Models.Info_Data info)
        {
            PushInfoReceiver.Instance.AddInfo(info);
        }

        public void RemoveInfo(long infoID, int categoryID)
        {
            PushInfoReceiver.Instance.RemoveInfo(infoID, categoryID);
        }

        #endregion

        #region IInfoReceiver 成员

        void IPushInfoReceiver.AddInfo(SAD.Common.Models.Info_Data info)
        {
            throw new NotImplementedException();
        }

        void IPushInfoReceiver.RemoveInfo(long infoID, int categoryID)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class PushInfoReceiver
    {
        private List<ServiceHost> m_Hosts = null;

        private InfoReceiverManager m_InfoReceiverManager;

        public Exception LastException { get; private set; }

        #region Instance
        /// <summary>
        /// 类型单例
        /// </summary>
        public static PushInfoReceiver Instance
        {
            get
            {
                return m_Instance;
            }
        }
        private static PushInfoReceiver m_Instance = new PushInfoReceiver();
        #endregion

        private PushInfoReceiver()
        {
        }

        public void Init(InfoReceiverManager irm)
        {
            m_InfoReceiverManager = irm;

            m_Hosts = new List<ServiceHost>();
            m_Hosts.Add(new ServiceHost(typeof(PushInfoReceiverService)));
        }

        public void Start()
        {
            int num = 0;

            //开启服务
            foreach (var host in m_Hosts)
            {
                num = 0;
                while (true)
                {
                    try
                    {
                        if (host.State != CommunicationState.Opened)
                        {
                            host.Open();
                        }
                        break;
                    }
                    catch (Exception ex)
                    {
                        num++;
                        if (num > MainActivator.Current.ReNum)
                        {
                            LastException = new Exception("启动失败，启动自身服务失败!");
                            return ;
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
        }

        public void Stop()
        {
            int num = 0;

            //关闭服务
            foreach (var host in m_Hosts)
            {
                num = 0;
                while (true)
                {
                    try
                    {
                        if (host.State != CommunicationState.Closed || host.State != CommunicationState.Faulted)
                        {
                            host.Close();
                        }
                        break;
                    }
                    catch (Exception ex)
                    {
                        num++;
                        if (num > MainActivator.Current.ReNum)
                        {
                            LastException = new Exception("停止失败，停止自身服务失败!");
                            return;
                        }
                        System.Threading.Thread.Sleep(100);
                    }
                }
            }
            this.m_Hosts.Clear();
            this.m_Hosts = null;
        }

        public void AddInfo(SAD.Common.Models.Info_Data info)
        {
            m_InfoReceiverManager.OnAddInfoEvent(info);
        }

        public void RemoveInfo(long infoID, int categoryID)
        {
            m_InfoReceiverManager.OnRemoveInfoEvent(infoID, categoryID);
        }
    }
}